const express = require('express')
const album = express.Router()

const axios = require('axios')
    //创建专辑
album.post('/album/creat', (req, res) => {
        let { userId, albumName } = req.query
        if (req.session.userMessage == undefined && userId == undefined) {
            res.status(200).json({
                data: '请先登录'
            })
        } else {
            axios({
                method: 'post',
                url: '/user/creatAlbum',
                params: {
                    userId: userId || req.session.userMessage.id,
                    albumName: albumName
                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    token:(req.session.token?req.session.token:'')
                }
            }).then(data => {
                console.log(data.data);
                res.status(200).json({
                    data: data.data
                })
            }).catch(err => {
                console.log(err);
                res.status(200).json({
                    err: err
                })
            })
        }
    })
    //得到用户专辑
album.get('/album/getAlbumList', (req, res) => {
        let { userId } = req.query
if (req.session.userMessage == undefined && userId == undefined) {
            res.status(200).json({
                data: '请先登录'
            })
        } else {
            axios({
                method: 'get',
                url: '/user/getAlbumList',
                params: {
                    userId: userId || req.session.userMessage.id,
                    token:(req.session.token?req.session.token:'')
                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                }
            }).then(data => {
            
                console.log(data.data);
                res.status(200).json({
                    data: data.data
                })
            }).catch(err => {
                console.log(err);
                res.status(200).json({
                    err: err
                })
            })
        }
    })
    //删除专辑
album.post('/album/deletealbum', (req, res) => {
    let { albumId } = req.query
    axios({
        method: 'post',
        url: '/index',
        params: {
            albumId: albumId
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        }
    }).then(data => {
        console.log(data.data);
        res.status(200).json({
            data: data.data
        })
    }).catch(err => {
        console.log(err);
        res.status(200).json({
            err: err
        })
    })
})



//个人中心查看粉丝数量
album.get('/personmainpage/fanone', (req, res) => {
    let { userId } = req.query
    axios({
        method: 'get',
        url: '/user/checkFollowers',
        params: {
            userId: userId
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        }
    }).then(data => {
        console.log(data.data);
        res.status(200).json({
            data: data.data
        })
    }).catch(err => {
        console.log(err);
        res.status(200).json({
            err: err
        })
    })
})


//通过专辑id查看专辑
album.get('/album/lookalbum', (req, res) => {
    console.log(req.query)
    let { albumId } = req.query

    axios({
        method: 'get',
        url: '/index/checkAlbumById',
        params: {
            albumId: albumId
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    }).then(data => {
        console.log(data.data);
        res.status(200).json({
            data: data.data
        })
    }).catch(err => {

        res.status(200).json({
            err: err
        })
    })
})

//阅读专辑页面，查看专辑收藏数量
album.get('/album/collection/num', (req, res) => {
    let { albumId } = req.query
    axios({
        method: 'get',
        url: '/index/collectAlbumCount',
        params: {
            albumId: albumId
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    }).then(data => {
        console.log(data.data);
        res.status(200).json({
            data: data.data
        })
    }).catch(err => {
        console.log(err);
        res.status(200).json({
            err: err
        })
    })
})
//查看专辑内的帖子
album.get('/album/checkForumByAlbumId', (req, res) => {
    let { albumId, pageNo, pageSize } = req.query
    axios({
        method: 'get',
        url: '/index/checkForumByAlbumId',
        params: {
            albumId: albumId,
            pageNo: pageNo ,
            pageSize: pageSize 
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    }).then(data => {
        res.status(200).json({
            data: data.data
        })
    }).catch(err => {
        res.status(200).json({
            err: err
        })
    })
})



module.exports = album